var t, e = getApp(), a = require("../../../utils/util.js");

Page({
    data: {
        hidden: !0,
        machineState: !0
    },
    onLoad: function(e) {
        if (t = this, void 0 !== e.id) wx.setStorageSync("autoId", e.id), this.setData({
            shopId: e.id
        }), t.getIsNewUser(); else {
            var a = wx.getStorageSync("autoId");
            this.setData({
                shopId: a
            });
        }
        t.checkLogin();
    },
    next: function(n) {
        var o = e.globalData.token;
        a.request(e, "wx/cust/hasPhone", "POST", {
            token: o
        }, function(e) {
            e.state && (null != e.data && null != e.data.phone && "" != e.data.phone ? t.data.machineState ? wx.navigateTo({
                url: "../prepare1/index?shopId=" + n.currentTarget.dataset.shopid
            }) : wx.showToast({
                title: "机器暂时无法使用",
                icon: "none"
            }) : t.setData({
                hidden: !1
            }));
        }, function(t) {});
    },
    onReady: function() {
        this.record = this.selectComponent("#record");
    },
    onHide: function() {
        this.record.pauseMuisc();
    },
    onShow: function() {
        t.checkLogin();
    },
    call: function() {
        wx.makePhoneCall({
            phoneNumber: "4006046606"
        });
    },
    getIsNewUser: function() {
        var n = e.globalData.token;
        a.request(e, "wx/cust/getIsCardTwice", "GET", {
            token: n,
            shopId: t.data.shopId
        }, function(e) {
            e.state && e.data.length > 0 && (t.setData({
                isNewUser: e.data[0].consumeNum
            }), e.data[0].consumeNum >= 2 && wx.showModal({
                title: "提示",
                content: "您是老用户，可以跳过指引教程",
                success: function(e) {
                    e.confirm ? wx.navigateTo({
                        url: "../prepare3/index?shopId=" + t.data.shopId
                    }) : e.cancel;
                }
            }));
        }, function(t) {});
    },
    getPhoneNumber: function(t) {
        var n = this;
        a.registerPhone(t.detail.encryptedData, t.detail.iv, e, function(t) {
            wx.showToast({
                title: "授权成功"
            });
        }), n.setData({
            hidden: !0
        });
    },
    confirm: function(t) {
        var e = this;
        wx.getStorageSync("UserPhone") ? (e.setData({
            hidden: !0
        }), 1 == e.data.clickInto ? e.order() : e.line()) : wx.showToast({
            title: "请先绑定手机",
            icon: "none"
        });
    },
    cancelPhone: function() {
        this.setData({
            hidden: !0
        });
    },
    pause: function() {},
    checkLogin: function() {
        wx.login({
            success: function(t) {
                t.code ? wx.request({
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    url: e.globalData.url + "wx/login",
                    data: {
                        code: t.code
                    },
                    success: function(t) {
                        if (100 == t.data.code) {
                            wx.setStorageSync("state", 2);
                            var a = getCurrentPages();
                            "pages/start/start" != a[a.length - 1].route && wx.redirectTo({
                                url: "/pages/start/start"
                            });
                        }
                        t.data.state ? (e.globalData.token = t.data.data.token, e.globalData.userInfo = t.data.data) : wx.showToast({
                            title: "请登录",
                            icon: "none"
                        });
                    }
                }) : console.log("获取用户登录态失败！" + t.errMsg);
            }
        });
    },
    getMachineIsReady: function() {
        var a = e.globalData.token;
        wx.request({
            url: e.globalData.url + "wx/car/machineIsReady",
            data: {
                token: a,
                shopId: t.data.shopId
            },
            success: function(e) {
                e.data.state ? t.setData({
                    machineState: !0
                }) : wx.showToast({
                    title: "机器暂时无法使用",
                    icon: "none"
                });
            },
            fail: function(t) {}
        });
    }
});